#include "t0022-0.h"
#include "cui.h"
#define MODNAME     "[DBG]"
// void cui_getexepath(char *path, char *name) {
//     char executable[4096];
//     ssize_t len;

//     // 读取符号链接 /proc/self/exe
//     len = readlink("/proc/self/exe", executable, sizeof(executable)-1);
//     if (len != -1) {
//         executable[len] = '\0'; // 添加字符串结束符

//         // 复制路径和名字
//         strcpy(path, executable);

//         // 提取程序名字
//         char *ptr = strrchr(executable, '/');
//         if (ptr != NULL) {
//             strcpy(name, ptr + 1);
//         } else {
//             strcpy(name, "");
//         }
//     } else {
//         perror("Error reading symlink");
//     }
// }

/*
* Debug functions
*/
void cui_dump_form(struct cui_widget_st *widget)
{
    if (widget) {
        struct cui_form_st* cform = widget->widgetinstance;
        if (cform) {
            int i;
            ERRSYS_INFOPRINT("WIDGET[%s]: x:%d y:%d rows:%d cols:%d flags:0x%x\n", widget->name, widget->beginx, widget->beginy, widget->lines, widget->cols, widget->flags);
            for (i = 0;i < cform->nfields;i ++) {
                struct cui_field_st *cfield = &cform->cfields[i];
                ERRSYS_INFOPRINT("LABEL[%s] h:%d w:%d x:%d y:%d\n", cfield->name, cfield->height, cfield->width_label, cfield->beginx_label, cfield->beginy_label);
                ERRSYS_INFOPRINT("FIELD[%s] h:%d w:%d x:%d y:%d\n", cfield->name, cfield->height, cfield->width_field, cfield->beginx_field, cfield->beginy_field);
            }
        }
    }
}

void cui_dump_menu(struct cui_widget_st *widget)
{
    // ERRSYS_INFOPRINT("!!!!!!!!!!!!!!\n");
    if (widget) {
        struct cui_menu_st* cmenu = widget->widgetinstance;
        if (cmenu) {
            int i;
            ERRSYS_INFOPRINT("WIDGET[%s]: x:%d y:%d rows:%d cols:%d flags:0x%x\n", widget->name, widget->beginx, widget->beginy, widget->lines, widget->cols, widget->flags);
            ERRSYS_INFOPRINT("MENU[%s]: x:%d y:%d rows:%d cols:%d mrows:%d mcols:%d\n", widget->name, cmenu->beginx, cmenu->beginy, cmenu->lines, cmenu->cols, cmenu->mrows, cmenu->mcols);
            for (i = 0;i < cmenu->nitems;i ++) {
                ERRSYS_INFOPRINT("name:%s\n", cmenu->name[i]);
            }
        }
    }
}

void cui_dump_widget_byname(struct cui_screen_st *cscr, const char *name)
{
    struct cui_widget_st *widget;
    widget = get_widget_byname(&cscr->windowlist, name);
    if (widget != NULL) {
        ERRSYS_INFOPRINT("WIDGET[%s]: x:%d y:%d rows:%d cols:%d flags:0x%x\n", widget->name, widget->beginx, widget->beginy, widget->lines, widget->cols, widget->flags);
        // ERRSYS_INFOPRINT("[%s]: initializer %p\n", widget->name, widget->ops.initialize);
    }
    else {
        ERRSYS_INFOPRINT("WIDGET[%s]: not found\n", name);
    }
}


void cui_dump_widget(struct cui_widget_st *widget)
{
    ERRSYS_INFOPRINT("WIDGET[%s]: x:%d y:%d rows:%d cols:%d flags:0x%x\n", widget->name, widget->beginx, widget->beginy, widget->lines, widget->cols, widget->flags);
    // ERRSYS_INFOPRINT("[%s]: initializer %p\n", widget->name, widget->ops.initialize);
    return;
}

void cui_dump_widget_hierarchy(struct cui_widget_st *widget)
{
    int nhierarchies = 0;
    char buf[1024] = {0};
    int sz = 1024;
    struct cui_widget_st *tmp = widget;
    // ERRSYS_INFOPRINT("%s begin\n", __func__);
    memset(buf, 0, 1024);
    while (widget != NULL) {
        // ERRSYS_INFOPRINT("%s nhierarchies %d parent %p\n", __func__, nhierarchies, widget->parent);
        widget = widget->parent;
        nhierarchies ++;
    }
    nhierarchies -= 1;
    ERRSYS_INFOPRINT("nhierarchies %d\n", nhierarchies);
    widget = tmp;
    if (nhierarchies != 0) {
        struct cui_widget_st *widget_stacks[32];
        int n = nhierarchies;
        if (n < 32) {
            memset(widget_stacks, 0, sizeof(struct cui_widget_st*)*32);
            while (widget != NULL) {
                // ERRSYS_INFOPRINT("index %d %p\n", n-1, widget);
                widget_stacks[n - 1] = widget;
                widget = widget->parent;
                n --;
            }
            for (n = 0;n < nhierarchies;n ++) {
                snprintf(buf + strlen(buf), sz - strlen(buf), "/%s", widget_stacks[n]->name);
            }
            ERRSYS_INFOPRINT("Widget hierachies: %s\n", buf);
        }
    }
    // ERRSYS_INFOPRINT("%s end\n", __func__);
}

void cui_dump_screen(struct cui_screen_st *cscr)
{
    if (cscr != NULL) {
        int i;
        ERRSYS_INFOPRINT("###### SCREEN STACK BEGIN ######\n");
        for (i = 0;i < cscr->stack.count;i ++) {
            ERRSYS_INFOPRINT("[%d] [%s]\n", i, cscr->stack.widgets[i]->name);
        }
        ERRSYS_INFOPRINT("###### SCREEN STACK END ######\n");
    }
}

const char* cui_strerror(int errnum)
{
    switch (errnum) {
        case E_OK:
            return "ok";
        case E_BAD_ARGUMENT:
            return "bad argument";
        case E_SYSTEM_ERROR:
            return "system error";
    }

    return "unknown error";
}